package net.vkits.api;

import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiParam;
import net.vkits.anjiamiao.application.OrderApplicationService;
import net.vkits.anjiamiao.domain.model.OriginalOrder;
import net.vkits.common.dto.HistoryOrder;
import net.vkits.common.dto.Order;
import org.dozer.DozerBeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-10-27T05:49:23.577Z")

@Controller
public class OrderApiController implements OrderApi {
    private static final Logger logger = LoggerFactory.getLogger(OrderApiController.class);

    @Resource
    private DozerBeanMapper mapper;
    @Resource
    private HttpServletRequest request;
    @Resource
    private OrderApplicationService orderApplicationService;

    public ResponseEntity<HistoryOrder> addOrder(@ApiParam(value = "订单原始金额" ,required=true ) @RequestBody Order order) {
        final Claims claims = (Claims) request.getAttribute("claims");
        String cellphone = claims.get("phone").toString();
        logger.info("get order is: {}", order);
        OriginalOrder originalOrder = mapper.map(order, OriginalOrder.class);
        net.vkits.anjiamiao.domain.model.Order domainOrder = orderApplicationService.addOrder(originalOrder, cellphone);
        HistoryOrder orderDTO = mapper.map(domainOrder, HistoryOrder.class);
        logger.info("new order is: {}", orderDTO);
        return new ResponseEntity<>(orderDTO, HttpStatus.OK);
    }

    public ResponseEntity<HistoryOrder> orderGet(@ApiParam(value = "过滤条件", required = true)
                                                 @RequestParam(value = "id", required = true) String id) {
        net.vkits.anjiamiao.domain.model.Order order = orderApplicationService.getOrder(id);
        logger.info("get order is: {}", order);
        HistoryOrder historyOrder = mapper.map(order, HistoryOrder.class);
        return new ResponseEntity<>(historyOrder, HttpStatus.OK);
    }

}
